package com.itheima.controller;


import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Slf4j 用来记录日志
 */
@Slf4j
@RestController
public class UploadController {


//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws Exception{
//        log.info("进行文件上传:{},{},{}",username,age,image);
//
//
//        String originalFilename = image.getOriginalFilename();
//        //将文件存储在本地目录
//        image.transferTo(new File("C:\\file\\image\\"+originalFilename));
//
//
//        return Result.success();
//
//    }


    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传:{}", image.getOriginalFilename());
        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成,文件访问的url:{}",url);

        //将图片上传完成后的url返回,用于浏览器回显展示
        return Result.success(url);
    }


}
